package com.jiaai.web.hb;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.jiaai.auth.model.AccessUser;
import com.jiaai.filter.aop.TokenIntercept;
import com.jiaai.member.api.IHbApi;
import com.jiaai.member.api.IWalletAPI;
import com.jiaai.util.RequestUtils;
import com.jiaai.util.RestResultGenerator;
import com.jiaai.util.code_pack.ErrorCode;
import com.jiaai.web.zfp.JMSProducer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.jms.Destination;
import java.math.BigDecimal;

/**
 * class_name:ZHbController
 * describe:--------------------->红包类, 注意要生成年月的数据库.
 *                                直播间结束后会发送结算该直播间内所有未领取完的红包
 * create_user:张昉平
 * create_date:2017/12/28
 * create_date:19:03
 */
@RestController
@RequestMapping("/api/v1/app/hb")
public class ZHbController {

    @Autowired
    private JMSProducer producer;
    @Reference
    private IWalletAPI iWalletAPI;
    @Reference
    private IHbApi iHbApi;

    @GetMapping("/list/{yearMonth}")
    @TokenIntercept(checkVisitor = true)
    public Object list(@PathVariable String yearMonth
            , @RequestParam(required = false, defaultValue = "1") Integer curr){
        AccessUser user = RequestUtils.getUser();

        try {
            return RestResultGenerator.genSuccessResult(iHbApi.getMyHbHis(user.getUserId(), yearMonth, curr));
        }catch (Exception e){
            e.printStackTrace();
            return RestResultGenerator.genErrorResult("服务器异常");
        }
    }

    @PostMapping("/publish")
    @TokenIntercept(checkVisitor = true)
    public Object publish(@RequestParam Integer liveId,
                          @RequestParam Integer hbMoney,
                          @RequestParam Integer getCondition,
                          @RequestParam Integer hbNum){
        AccessUser user = RequestUtils.getUser();

        try {
            BigDecimal money = iWalletAPI.getMyMoney(user.getUserId());
            if (money.intValue() < hbMoney){
                return RestResultGenerator.genErrorResult(ErrorCode.NOT_ENOUGH_MONEY, "余额不足");
            }
            Destination des = new ActiveMQQueue("publishHb.queue");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("liveId", liveId);
            jsonObject.put("sendId", user.getUserId());
            jsonObject.put("hbMoney", hbMoney);
            jsonObject.put("getCondition", getCondition);
            jsonObject.put("hbNum", hbNum);
            jsonObject.put("nickName", user.getNickName());
            jsonObject.put("mId", user.getUserId());
            Integer hbId = producer.publishHb(des, jsonObject.toJSONString());
            return RestResultGenerator.genSuccessResult(hbId);
        }catch (Exception e){
            e.printStackTrace();
            return RestResultGenerator.genErrorResult("服务器异常");
        }
    }

    //主播的anchorId也传过来
    @PostMapping("/snatch")
    @TokenIntercept(checkVisitor = true)
    public Object snatch(@RequestParam Integer hbId
            , @RequestParam String anchorId){
        AccessUser user = RequestUtils.getUser();

        Destination ds = new ActiveMQQueue("snatchHb.queue");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mId", user.getUserId());
        jsonObject.put("anchorId", anchorId);
        jsonObject.put("hbId", hbId);
        String res = producer.snatchHb(ds, jsonObject.toJSONString());
        switch (res){
            case "0":
                return RestResultGenerator.genErrorResult(ErrorCode.HB_NO_REST, "红包已被领取完了!");
            case "1":
                return RestResultGenerator.genErrorResult(ErrorCode.NOT_FANS, "您不是该主播的粉丝,不能领取!");
            case "2":
                return RestResultGenerator.genErrorResult(ErrorCode.HB_NO_REST,"红包已被领取完了!");
            default:
                return RestResultGenerator.genSuccessResult(JSONObject.parseObject(res));
        }
    }

}
